package object;

import utilities.ImageLoader;
import utilities.ImagePlayer;
import java.awt.Graphics;
import java.awt.*;
import java.awt.image.*;

/**
 * Class containing about the Sprite object, from which the Player and enemies are descended
 *
 */

public abstract class Sprite
{
        private static final int XSTEP=5;
        private static final int YSTEP=5;
        private static final int SIZE=12;

        private int width,height; //image dimensions
        private int pWidth,pHeight; //panel dimensions

        private ImageLoader imLoader;
        private ImagePlayer imPlayer;
        private BufferedImage bufImage;

        private String imageName;

        private boolean isActive=true;
        private boolean isLooping;
        
        protected int locx,locy; //location of the sprite
        protected int dx,dy; //amount to move

        //Creates a new Sprite
	public Sprite(int x,int y,int width,int height,ImageLoader im,String name)
        {
                locx=x;
                locy=y;
                pWidth=width;
                pHeight=height;
                dx=XSTEP;
                dy=YSTEP;
                imLoader=im;
                setImage(name);
	}

        //Set a certain image for the Sprite from the map
        public void setImage(String name)
        {
                imageName=name;
                bufImage=imLoader.getImage(imageName);
                if (bufImage==null)
                {
                    System.out.println("No sprite image for " + imageName);
                    width=SIZE;
                    height=SIZE;
                }
                else
                {
                    width=bufImage.getWidth();
                    height=bufImage.getHeight();
                }
                imPlayer=null;
                isLooping=false;
        }

        public int getPWidth()   // of the enclosing panel
        {
            return pWidth;
        }

        public int getPHeight()  // of the enclosing panel
        {
            return pHeight;
        }

	public int getWidth()
        {
            return width;
	}

	public int getHeight()
        {
            return height;
	}
        
        public boolean isActive()
        {
            return isActive;
        }

        public void setActive(boolean bool)
        {
            isActive=bool;
        }

        //Sets the coordinates of the Sprite in the world
        public void setPosition(int x,int y)
        {
            locx=x;
            locy=y;
        }

        //Translates the Sprite in the world
        public void translate(int x,int y)
        {
            locx+=x;
            locy+=y;
        }

        //Return the X location coordinate of the Sprite
        public int getXPos()
        {
            return locx;
        }

        //Return the Y location coordinate of the Sprite
        public int getYPos()
        {
            return locy;
        }

        public Rectangle getRectangle()
        {
            return  new Rectangle(locx,locy,width,height);
        }

        //Sets the step size the Sprie can make
        public void setStep(int x,int y)
        {
            dx=x;
            dy=y;
        }

        //Loops a sequence of images
        public void loopImage(int animPeriod, double seqDuration)
        {
            if (imLoader.numberOfImages(imageName)>1)
            {
                imPlayer=null;
                imPlayer=new ImagePlayer(imageName,animPeriod,seqDuration,true,imLoader);
                isLooping=true;
            }
            else
            System.out.println(imageName + " is not a sequence of images");
        }

        //Stops the image looping
        public void stopLooping()
        {
            if (isLooping)
            {
                imPlayer.stop();
                isLooping=false;
            }
         }

        public void updateSprite()
        {
            if (isActive())
            {
                locx+=dx;
                locy+=dy;
                if (isLooping)
                    imPlayer.updateTick();
            }
        }

	public void drawSprite(Graphics g)
        {
            if (isActive())
            {
                if (bufImage==null)
                {
                    g.setColor(Color.red);
                    g.fillRect(locx,locy,SIZE,SIZE);
                    g.setColor(Color.black);
                }
                else
                {
                    if (isLooping)
                    bufImage=imPlayer.getCurrentImage();
                    g.drawImage(bufImage,locx,locy,null);
                }
            }
        }
}

